
//  HMTopView.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/8.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit
import SnapKit

// 公共的间距
let HMStatusCellMargin: CGFloat = 8
// 头像宽高
let HMStatusCellHeadImageWH: CGFloat = 35

class HMTopView: UIView {
    
    var cons:Constraint?
    
    var statusViewModel: HMCellViewModel? {
        
        didSet {

            // 显示的小图片
            if let strUrl = statusViewModel?.status?.user!.profile_image_url {
                if let url = NSURL(string: strUrl){
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default" ))
                }
            }
            
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            // 设置时间和来源 TODO: 以后会专门处理
            timeLabel.text = statusViewModel?.created_Sum
            // 设置认证图标
            verifiedImage.image = statusViewModel?.verifiedImage
            // 设置vip图标
            membershipImage.image = statusViewModel?.membershipImage
            // 设置内容
            contentLabel.text = statusViewModel?.status?.text
            // 微博来源
            comeLabel.text = statusViewModel?.source_Sum
        
            cons?.uninstall()
            // TODO: 根据是否有配图去显示或者隐藏配图视图
            if let pic = statusViewModel?.status?.pic_urls where pic.count > 0 {
                // 有配图，显示界面，更新约束，赋值
                
                // 赋值
                pictureCollectionView.pictData = pic
                
                // 没有配图，隐藏界面，更新约束
                pictureCollectionView.hidden = false
                   
                self.snp_makeConstraints { (make) in
                    
                    self.cons = make.bottom.equalTo(pictureCollectionView).offset(HMStatusCellMargin).constraint
                }
                
    
            }else {
                // 没有配图，隐藏界面，更新约束
                pictureCollectionView.hidden = true

                self.snp_makeConstraints { (make) in
                    
                    self.cons = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                }
                
            }
        }
        
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        // 添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(comeLabel)
        addSubview(contentLabel)
        addSubview(pictureCollectionView)
        addSubview(verifiedImage)
        addSubview(membershipImage)
        
        
        
        // 更新约束
        
        iconView.snp_makeConstraints { (make) in
         
            make.top.leading.equalTo(self).offset(HMStatusCellMargin)
            make.size.equalTo(CGSizeMake(HMStatusCellHeadImageWH, HMStatusCellHeadImageWH))
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(iconView.snp_trailing).offset(HMStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(iconView.snp_trailing).offset(HMStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        comeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        verifiedImage.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_trailing).offset(-3)
            make.centerY.equalTo(iconView.snp_bottom).offset(-3)
            
        }
        
        membershipImage.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            
        }
        
        // 微博内容 上坐下对齐，高度自定义
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(HMStatusCellMargin)
            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        pictureCollectionView.snp_makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            
        }
        
        self.snp_makeConstraints { (make) in
            
           cons = make.bottom.equalTo(pictureCollectionView).offset(HMStatusCellMargin).constraint
        }
        
    }
    
    // MARK: -懒加载
    // 左上图片
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named:"avatar_default"))
    // 昵称
    private lazy var nameLabel:UILabel = {
       let v = UILabel(title: "文艺青年", color: UIColor.darkGrayColor())
        v.font = UIFont.systemFontOfSize(15)
       return v
        
    }()
    // 时间
    private lazy var timeLabel:UILabel = {
        let v = UILabel(title: "刚刚", color: UIColor.orangeColor())
        v.font = UIFont.systemFontOfSize(12)
        return v
        
    }()
    // 来源
    private lazy var comeLabel:UILabel = {
        let v = UILabel(title: "来自微博", color: UIColor.darkGrayColor())
        v.font = UIFont.systemFontOfSize(12)
        return v
    }()
    // 微博内容
    private lazy var contentLabel:UILabel = {
        let v = UILabel(title: "微博内容", color: UIColor.darkGrayColor())
        v.numberOfLines = 0
        v.font = UIFont.systemFontOfSize(14)
        return v
    }()
    // 认证
    private lazy var verifiedImage: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "avatar_vip")
        return imageView
    }()
    // 会员图标
    private lazy var membershipImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    // 微博配图
    private lazy var pictureCollectionView: HMPictureCollectionView = HMPictureCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
    

}
